package com.grantheath.erebus.controller.transitionManagers.linearTransitionManagers
{
	import com.grantheath.erebus.model.enum.notifications.ErebusPageNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusTransitionNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusPageLoaderNotifications;
	import com.grantheath.erebus.model.interfaces.IErebusAssetNode;
	import com.grantheath.erebus.model.enum.notifications.ErebusAssetLoaderNotifications;
	import com.grantheath.erebus.model.interfaces.IErebusSiteTreeProxy;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;
	import com.grantheath.erebus.model.enum.names.ErebusProxyNames;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	/**
	 * @author grantheath
	 */
	public class ErebusPageLoadCompleteCommand extends SimpleCommand implements ICommand 
	{
		override public function execute(notification : INotification) : void
		{
			var siteTreeProxy : IErebusSiteTreeProxy = facade.retrieveProxy(ErebusProxyNames.SITE_TREE_PROXY) as IErebusSiteTreeProxy;
			var pageNode : IErebusPageNode = notification.getBody() as IErebusPageNode;
			var assetNode : IErebusAssetNode;
			
			trace("page loaded: " + pageNode);
			siteTreeProxy.registerLoadedPageNode(pageNode);
			siteTreeProxy.assetQueue = pageNode.pageAssets;
			assetNode = siteTreeProxy.assetQueue[0] as IErebusAssetNode;
			if(assetNode){
				sendNotification(ErebusAssetLoaderNotifications.LOAD_ASSET, assetNode);
			} else {
				sendNotification(ErebusPageNotifications.INITIALIZE_PAGE, pageNode);
				pageNode = siteTreeProxy.incrementLoadQueue();
				
				if(pageNode){
					sendNotification(ErebusPageLoaderNotifications.LOAD_PAGE, pageNode);
				} else {
					sendNotification(ErebusTransitionNotifications.PAGE_LOAD_PROCESS_COMPLETE);
					sendNotification(ErebusTransitionNotifications.PAGE_INITIALIZE_PROCESS_COMPLETE);
				}
			}
		}
	}
}